package sj;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<Student> stu = Student.class;
        System.out.println(stu);//类名

        int modifiers = stu.getModifiers();//修饰符
        if ((modifiers & Modifier.PUBLIC)==Modifier.PUBLIC){
            System.out.println("public");
        }

        System.out.println(stu.getSuperclass().getName());//继承的类
        System.out.println(stu.getConstructor());//构造方法
        Field name = stu.getDeclaredField("name");//属性
        name.setAccessible(true);
        System.out.println(name);
        System.out.println(stu.getMethod("show"));//方法

        //取值
        Student student = stu.newInstance();
        System.out.println("name值："+name.get(student));
        //赋值
        name.set(student,"周周");
        System.out.println(name.get(student));

        //方法名和参数列表获取静态方法
        Method getAge = stu.getDeclaredMethod("getAge",null);
        getAge.setAccessible(true);
        System.out.println(getAge.invoke("年龄："+student));
        //方法名和参数列表获取静态方法
        Method show = stu.getDeclaredMethod("show");
        show.setAccessible(true);
        System.out.println(show.invoke(student));
    }
}
